﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Collections;
using GPCSC.Web;

namespace GPCSC.Web.SYS_ORG
{
    public partial class PostList : PageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                f_buildGrid();
            }
        }
        protected void f_buildGrid()
        {
            Hashtable htable = new Hashtable();
            DataSet ds = new DataReturn().ReturnData(htable, "sel_POST_all");

            if (ds != null)
            {
                if (ds.Tables[0].Rows.Count > 0)
                {
                    foreach (DataRow dbRow in ds.Tables[0].Rows)
                    {
                        if (dbRow["EditUrl"] != null)
                        {
                            dbRow["EditUrl"] = "PostEdit.aspx?Command=Edit";
                        }

                        if (dbRow["ViewUrl"] != null)
                        {
                            dbRow["ViewUrl"] = "PostEdit.aspx?Command=View";
                        }
                    }
                }
                PostListS.DataSource = ds;
                PostListS.DataBind();
                this.pageinfo.InnerHtml= new FYpage().GetPageNum(ds.Tables[0],PostListS,10);
               
            }
        }
        /// <summary>
        /// Callback异步加载数据
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void LCallBack_Callback(object sender, NickLee.Web.UI.CallBackEventArgs e)
        {
            SqlReadXml org = new SqlReadXml();
            try
            {
                #region 删除数据
                if (Usual.f_convertString(e.Parameters[0]) == "3" )
                {
                    SqlReadXml Org = new SqlReadXml();
                    var allname = "";
                    var deptname = "";
                    try
                    {
                        
                            var zid = Usual.f_convertString(e.Parameters[2]);
                            if (zid == "" || zid == null)
                            {
                               
                            }
                            base.o_uiTable.Clear();
                            base.o_uiTable.Add("@POSTID", zid);

                            //先查找有无子部门
                            DataSet dset = new DataSet();
                            dset = Org.ReaderDataSet("sel_postchild_info", base.o_uiTable);
                            if (dset.Tables[0].Rows.Count > 0)
                            {
                                //Response.Write("<script>window.alert('要删除的部门有子部门，请先删除他的子部门！');</script>");
                                deptname = deptname + dset.Tables[0].Rows[0]["PARPOSTNAME"].ToString() + ",";
                                
                            }

                            //再查找有无用户
                            dset = Org.ReaderDataSet("sel_postuser_info", base.o_uiTable);
                            if (dset.Tables[0].Rows.Count > 0)
                            {
                                //Response.Write("<script>window.alert('要删除的部门有用户，请先删除他的用户！');</script>");
                                deptname = deptname + dset.Tables[0].Rows[0]["POSTNAME"].ToString() + ",";
                              
                            }

                            //删除此部门
                            if (deptname == "")
                            {
                                Org.NotDoSQL("delCL_POSTID", base.o_uiTable);

                            }

                       
                        //如果所选择要删除的部门里,没有部门拥有用户或是子部门
                        if (deptname == "")
                        {

                            
                            hfError.Value = lblProcesserMsg.Text = "删除数据成功!";

                        }
                        else
                        {

                           
                            hfError.Value = lblProcesserMsg.Text = "删除数据失败!" + System.Environment.NewLine + "原因:" + deptname + "拥有用户或是子部门!";
                           
                        }
                        this.f_buildGrid();

                    }
                    catch (System.Exception ee)
                    {

                      
                        hfError.Value = lblProcesserMsg.Text = "删除数据失败!" + ee.Message;
                       
                    }
                }
                #endregion


                #region 上移数据
                if (Usual.f_convertString(e.Parameters[0]) == "1")
                {
                    int number = Convert.ToInt32(Usual.f_convertString(e.Parameters[1]));
                    if (number != 1)
                    {
                        Guid cl_post_soid = new Guid(Usual.f_convertString(e.Parameters[2]));
                        int upnumber = number - 1;
                        Hashtable ht = new Hashtable();
                        ht.Add("@Number", number);
                        ht.Add("@upnumber", upnumber);
                        int ii = org.NotDoSQL("up_postNumber", ht);

                        if (ii == 1)
                        {
                            Hashtable htt = new Hashtable();
                            htt.Add("@Number", number);
                            htt.Add("@CL_POST_SOID", cl_post_soid);
                            org.NotDoSQL("up_postNumberNext", htt);
                        }
                    }
                    this.f_buildGrid();

                }
                #endregion


                #region 下移数据
                if (Usual.f_convertString(e.Parameters[0]) == "2")
                {
                    int maxnumber = 0;
                    Hashtable htable = new Hashtable();
                    DataSet dss = org.ReaderDataSet("sel_PostmaxNumber", htable);
                    if (dss != null && dss.Tables[0].Rows.Count != 0)
                    {
                        maxnumber = Convert.ToInt32(dss.Tables[0].Rows[0]["maxNumber"].ToString());
                    }
                    int number = Convert.ToInt32(Usual.f_convertString(e.Parameters[1]));
                    Guid cl_post_soid = new Guid(Usual.f_convertString(e.Parameters[2]));
                    if (number != maxnumber)
                    {
                        int upnumber = number + 1;
                        Hashtable ht = new Hashtable();
                        ht.Add("@Number", number);
                        ht.Add("@upnumber", upnumber);
                        int ii = org.NotDoSQL("up_postNumber", ht);

                        if (ii == 1)
                        {
                            Hashtable htt = new Hashtable();
                            htt.Add("@Number", number);
                            htt.Add("@CL_POST_SOID", cl_post_soid);
                            org.NotDoSQL("up_postNumberNextAdd", htt);
                        }
                    }
                    this.f_buildGrid();
                }

                 if (Usual.f_convertString(e.Parameters[0]) == "5")
                {
                    SqlReadXml Org = new SqlReadXml();//
                    var zid = Usual.f_convertString(e.Parameters[2]).ToString();
                    if (zid == "" || zid == null)
                    {
                        this.f_buildGrid();
                    }
                    else
                    {
                        string likes = e.Parameters[2].ToString().Trim();
                        likes = "%" + likes + "%";
                        Hashtable htable = new Hashtable();
                        htable.Add("@like", likes);
                        DataSet ds = Org.ReaderDataSet("sel_POST_bylike", htable);
                        if (ds.Tables[0].Rows.Count > 0)
                        {
                            foreach (DataRow dbRow in ds.Tables[0].Rows)
                            {
                                if (dbRow["EditUrl"] != null)
                                {
                                    dbRow["EditUrl"] = "PostEdit.aspx?Command=Edit";
                                }

                                if (dbRow["ViewUrl"] != null)
                                {
                                    dbRow["ViewUrl"] = "PostEdit.aspx?Command=View";
                                }
                            }
                        }
                        PostListS.DataSource = ds;
                        PostListS.DataBind();
                        this.pageinfo.InnerHtml = new FYpage().GetPageNum(ds.Tables[0], PostListS, 10);

                    }
                }

                #endregion

            }
            catch (System.Exception pexp)
            {
                throw (pexp);
            }
            //最后进行Grid数据查询并填充
            finally
            {
             
                LPanel.RenderControl(e.Output);
            }
        }
        
    }
}